package com.jbm.enterapplication.registeruser.view

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import com.jbm.core.common.MainServer
import com.jbm.enterapplication.R
import com.jbm.enterapplication.activity.MainActivity
import com.jbm.enterapplication.activityhome.view.OpenDoor
import com.jbm.enterapplication.sharedprefer.SharedPreActivity
import com.jbm.enterapplication.utlisclass.ExitActivity
import com.jbm.enterapplication.utlisclass.HttpNet
import com.jbm.enterapplication.view.DialogLoading
import com.jbm.enterapplication.view.DoubleButtonDialog
import com.jbm.onehome.common.OHInterface
import com.jbm.onehome.iface.Login
import kotlinx.android.synthetic.main.activity_photo_login_pwass.*
import kotlinx.android.synthetic.main.lz_titlehurdle.*
import rx.Observable
import rx.Observer

class PhotoLoginPwass : MainActivity(), View.OnClickListener {
    var loginid: Int? = null
    var Loginname: String? = null
    var sharedpre: SharedPreActivity? = null
     var  dialogLoading: DialogLoading?=null

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.register_manage_button_queding -> {
                if (til_srmim.editableText.toString().equals("")) {
                    til_srmims.error = "密码不能为空"
                } else if (tiet_password_ms.editableText.toString().equals("")) {
                    til_password_s.error = "密码不能为空"

                } else if (!til_srmim.editableText.toString().equals(tiet_password_ms)) {
                    til_srmims.error = "密码不一致"
                    til_password_s.error = "密码不一致"
                } else {
                    dialogLoading = DialogLoading(this, "加载中.....", false)
                    dialogLoading?.show()
                    HttpNet.getInstans().obsevice(Observable.create(Observable.OnSubscribe<Login> { obVersion ->
                        obVersion.onNext(HttpNet.getInstans().getOhInter().RegistCustomer(loginid!!, "", til_srmim.editableText.toString(), til_srmim.editableText.toString(), "", false, ""))
                    })).subscribe(object : Observer<Login> {
                        override fun onNext(t: Login?) {
                            if (t?.m_code == 200) {
                                setDialogding()
                                //验证成功
                                sharedpre?.setIsUser(Loginname!!, til_srmim.editableText.toString())//保存账号密码
                                sharedpre?.setIsConn(true)//连接网络成功
                                sharedpre?.setIsLogin(true)//登录成功
                                sharedpre?.setLoginId(OHInterface.getG_server().login_id.toString())//保存登录id
                                MainServer.getG_server().m_local_id = OHInterface.getG_server().login_id
                                startActivity(Intent(this@PhotoLoginPwass, OpenDoor::class.java))
                                finish()
                            } else if (t?.m_code == 500) {
                                //密码设置验证失败
                                setDialogding()
                                hint(t!!.m_info)
                            } else {
                                //密码设置验证失败
                                setDialogding()
                                hint(t!!.m_info)
                            }
                        }

                        override fun onError(e: Throwable?) {
                        }

                        override fun onCompleted() {
                        }

                    })

                }
            }
            R.id.teleft->{

                finish()

            }



        }

    }

    override fun getView(): Int {
        return R.layout.activity_photo_login_pwass
    }


    override fun getinit() {
        ExitActivity.getInstanst().addActivity(this)
        register_manage_button_queding.setOnClickListener(this)
        loginid = intent.getIntExtra("login_id", 0)
        Loginname = intent.getStringExtra("ed_phone_sjh")//手机号进行保存
        til_srmim.addTextChangedListener(MyWatcher(til_srmim))
        tiet_password_ms.addTextChangedListener(MyWatcher(tiet_password_ms))
        sharedpre = SharedPreActivity(this)
        teleft.setOnClickListener(this)
        tetitle.text="输入密码"
    }


    inner internal class MyWatcher : TextWatcher {
        var view: View? = null

        constructor(view: View?) {
            this.view = view
        }

        override fun afterTextChanged(s: Editable?) {
            when (view?.id) {
                R.id.til_srmim -> {
                    if (!s.toString().equals(""))
                        til_srmims.isErrorEnabled = false
                }
                R.id.tiet_password_ms -> {
                    if (!s.toString().equals(""))
                        til_password_s.isErrorEnabled = false
                }
            }

        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

        }

    }
    fun setDialogding() {
        if (dialogLoading != null) {
            dialogLoading!!.dismiss()
        }
    }
    //一些提示的时候调用
    fun hint(name: String) {
        val dialog = DoubleButtonDialog(this@PhotoLoginPwass, name)
        dialog.getokbtn().setOnClickListener(View.OnClickListener { dialog.dismiss() })
        dialog.setisVisibility()

    }

}
